package com.yxhl.zoume.core.tripsmgmt.presenter.zoumebusticket;

import android.content.Context;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.info.TicketCodeInfo;
import com.yxhl.zoume.core.tripsmgmt.info.ZMPassenger;
import com.yxhl.zoume.core.tripsmgmt.view.zoumebusticket.ZouMeBusTicketView;
import com.yxhl.zoume.data.http.model.account.ZMAccount;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.ZouMeBusTicketUseCase;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by vhawk on 16/6/16.
 */
public class ZouMeBusTicketPresenter implements IPresenter {

    private ZouMeBusTicketUseCase useCase;
    private ZouMeBusTicketView view;

    @Inject
    public ZouMeBusTicketPresenter(ZouMeBusTicketUseCase useCase) {
        this.useCase = useCase;
    }

    @Override
    public void attachView(BaseView view) {
        this.view = (ZouMeBusTicketView) view;
    }

    public void getZouMeBusTicketList(Context context, String orderId) {
        CommonReqParams.Builder commReqBuilder = CommonReqParams.newBuilder();
        commReqBuilder.setClientType(ClientType.USER).setToken(getToken(context));

        BizOrderRequest bizOrderRequest = BizOrderRequest.newBuilder()
                .setCommonReqParams(commReqBuilder)
                .setOrderSerial(orderId)//订单号
                .setMobile(getMobile(context))
                .build();

        useCase.execute(bizOrderRequest, new ZMSubscriber<BizOrderResponse>() {
            @Override
            public void onNext(BizOrderResponse response) {
                super.onNext(response);
                if ("true".equals(response.getCommonRespParams().getIsSucc())) {
                    List<BizOrder> orderFieldsesList = response.getOrderFieldsesList();
                    TicketCodeInfo info = fromOrderFieldList(orderFieldsesList);
                    view.handleZouMeBusTicketSucceed(info);
                } else {
                    view.handleZouMeBusTicketFailure(response.getCommonRespParams().getResultMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.handleZouMeBusTicketNetError();
            }
        });
    }

    /**
     * 数据适配
     *
     * @param orderFieldsesList
     * @return
     */
    private TicketCodeInfo fromOrderFieldList(List<BizOrder> orderFieldsesList) {
        TicketCodeInfo info = null;
        if (!CollectionUtils.isEmpty(orderFieldsesList)) {
            info = new TicketCodeInfo();
            BizOrder firstOrder = orderFieldsesList.get(0);

            String[] times = firstOrder.getGmtDepart().split(" ");
            info.setDepartTime(times[1])
                    .setDepartDate(times[0])
                    .setStartStation(firstOrder.getStart())
                    .setArriveStation(firstOrder.getDestination());

            List<String> ticketCodeList = new ArrayList<>();
            List<ZMPassenger> passengerList = new ArrayList<>();

            for (BizOrder order : orderFieldsesList) {
                ticketCodeList.add(order.getVerifyCode());

                ZMPassenger passenger = new ZMPassenger();
                passenger.setIdNumber(order.getIdNumber());
                passenger.setName(order.getUserName());
                passenger.setMobile(order.getMobile());
                passengerList.add(passenger);
            }
            info.setTicketCodeList(ticketCodeList)
                    .setPassengerList(passengerList);
        }

        return info;
    }


    protected String getMobile(Context context) {
        ZMAccount account = AccountUtils.getInstance(context).getAccount();
        String mobile;
        if (account != null) {
            mobile = account.getAccountName();
        } else {
            mobile = "";
        }
        return mobile;
    }


    protected String getToken(Context context) {
        ZMAccount account = AccountUtils.getInstance(context).getAccount();
        String token;
        if (account != null) {
            token = account.getAccessToken();
        } else {
            token = "";
        }
        return token;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        useCase.unsubscribe();
    }
}
